//表格初始化
$(document).ready(function() {
    $('#charactor_table').DataTable( {
        // fixedHeader: true
    } );
} );
//展示方法
let optionJson = [];
function user_charactor(){
    let request_parameters = {type:"get_all_user_info"};
    $.ajax({
        type:"post",
        url:"http://127.0.0.1:9998/show/get_all_user_info",
        data:request_parameters,
        async: false,
        success:function (all_user_info) {
              console.log(all_user_info);
            optionJson=all_user_info;
            $('#charactor_table').dataTable().fnDestroy();//还原初始化
            $('#charactor_table').DataTable({
                // "serverSide":true,
                //  "paging":true,
                // "lengthMenu":[15,25,35,100,25],
                // "pagingType":"scrolling",
                // serverSide:true,
                // Processing:true,
                // deferRender:true,
                "bStateSave":true,//翻页时保存状态
                // iDisplayLength:10,
                // iDisplayStart:0,
                "ordering":false,//禁用全局排序
                "sort":"position",
                data:optionJson.data,
                //设置列数据
                columns:[
                    {"data": 'phoneNum',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {"data": 'city',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {"data": 'keyword',
                    },
                    {"data": 'isSensitiveUser',
                        "render":function (data,type,row,meta) {
                        if(data=="N"){
                            return data = '<p style="text-align: center">否</p>'
                        }
                            else{
                            return data = '<p style="text-align: center">是</p>'
                        }
                        }
                    },
                    {"data": 'isOverseasUser',
                        "render":function (data,type,row,meta) {
                            if(data=="N"){
                                return data = '<p style="text-align: center">否</p>'
                            }
                            else{
                                return data = '<p style="text-align: center">是</p>'
                            }
                        }
                    },
                    {"data": 'userLabel',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    }
                ],
                //设置排序
                "order":[[1,'asc']],
                //设置语言
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项搜索结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "未搜索到数据",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
            });
        }
    });
}

function user_charactor_Excelexport() {
    //输出base64编码
    //要导出的json数据
    const jsonData =optionJson.data;
    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    let str = `电话号码,所在城市,用户关键词,是否是敏感用户,是否是海外用户,用户标签\n`;
    //增加\t为了不让表格显示科学计数法或者其他格式
    for(let i in jsonData ){
        let str1 = `${jsonData[i].phoneNum + '\t'},`;
        let str2 = `${jsonData[i].city + '\t'},`;
        let str3 = `${jsonData[i].keyword + '\t'},`;
        str3=str3.replace(/,/g, "、")+',';
        let str4 = `${jsonData[i].isSensitiveUser + '\t'},`;
        let str5 = `${jsonData[i].isOverseasUser + '\t'},`;
        let str6 = `${jsonData[i].userLabel + '\t'},`;
        str+=str1+str2+str3+str4+str5+str6+'\n';
    }
    //encodeURIComponent解决中文乱码
    let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
    //通过创建a标签实现
    let link = document.createElement("a");
    link.href = uri;
    //对下载的文件命名
    link.download =  "电信特征表.xls";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}